const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const webpack = require('webpack');

const PATHS = {
  app: path.join(__dirname, 'app'),
  build: path.join(__dirname, 'build'),
};

const commonConfig={
 entry: {
    app: PATHS.app,
  },
  output: {
    path: PATHS.build,
    filename: '[name].js',
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'Webpack demo',
    }),
  ],
}
 
function developmentConfig(){
  const config ={
    devServer:{
      //使能历史记录api
      historyApiFallback:true,
       hot:true,
       stats:'errors-only',
      host:process.env.Host,
      port:process.env.PORT,
      overlay:{
        errors:true,
        warnings:true,
      }
    },
     plugins: [
      new webpack.HotModuleReplacementPlugin(),
      new webpack.NamedModulesPlugin(),
    ],
  };
   return Object.assign(
    {},
    commonConfig,
    config,
    {
      plugins: commonConfig.plugins.concat(config.plugins),
    }
  );
}

module.exports = function(env){
  console.log("env",env);
  if(env=='development'){
    return developmentConfig();
  }
   return commonConfig;
};

/*"devDependencies": {
    "html-webpack-plugin": "^2.28.0",
    "nodemon": "^1.17.4",
    "webpack": "^2.2.1",
    "webpack-dev-server": "^2.4.1"
  }
    */